<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="凭证时间">
        <el-date-picker
          v-model="daterangeProofTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="凭证字" prop="proofWords">
        <el-input
          v-model="queryParams.proofWords"
          placeholder="请输入凭证字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会计科目" prop="accountingAccount">
        <el-input
          v-model="queryParams.accountingAccount"
          placeholder="请输入会计科目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="制单人" prop="documenter" >
        <el-input
          v-model="queryParams.documenter"
          placeholder="请输入制单人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核人" prop="auditor">
        <el-input
          v-model="queryParams.auditor"
          placeholder="请输入审核人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['proof:proof:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['proof:proof:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['proof:proof:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['proof:proof:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="proofList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="凭证编号" align="center" prop="proofId" />
      <el-table-column label="凭证时间" align="center" prop="proofTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.proofTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="摘要" align="center" prop="abstractContent" />
      <el-table-column label="凭证字" align="center" prop="proofWords" />
      <el-table-column label="会计科目" align="center" prop="accountingAccount" />
      <el-table-column label="借方金额" align="center" prop="debitAmount" />
      <el-table-column label="贷方金额" align="center" prop="creditAmount" />
      <el-table-column label="附件" align="center" prop="attachments" />
      <el-table-column label="制单人" align="center" prop="documenter" />
      <el-table-column label="审核人" align="center" prop="auditor" />
      <el-table-column label="凭证状态" align="center" prop="proofStatus" />
      <el-table-column label="录入时间" align="center" prop="entryTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['proof:proof:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['proof:proof:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改凭证设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body>

      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item label="凭证时间" prop="proofTime">
          <el-date-picker clearable
                          v-model="form.proofTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择凭证时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="凭证字" prop="proofWords">
<!--          <el-input v-model="form.proofWords" placeholder="请输入凭证字" />-->
          <el-select
            v-model="form.proofWords"
            placeholder="请选择"
            clearable filterable
            @blur="selectMethod">

            <el-option
              v-for="item in wordData"
              :key="item.certificateWord"
              :label="item.certificateWord"
              :value="item.certificateWord" />
          </el-select>
        </el-form-item>

        <el-form-item label="附件" prop="attachments">
          <el-input v-model="form.attachments" placeholder="请输入附件" />
        </el-form-item>
        <el-form-item label="制单人" prop="documenter">
          <el-input v-model="form.documenter" placeholder="请输入制单人" disabled/>
        </el-form-item>
        <el-form-item label="录入时间" prop="entryTime">
          <el-date-picker clearable
                          v-model="form.entryTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择录入时间"
                          disabled>
          </el-date-picker>
        </el-form-item>
      </el-form>
      <el-button @click="AddTable">+</el-button>
      <el-table
        :data="tableData"
        border
        height="200"
        :summary-method="getSummaries"
        show-summary
        style="width: 100%; height: 100%; margin-top: 20px">
        <el-table-column
          prop="id"
          label="序号"
          width="70px"
        >
          <template slot-scope="scope">
            <span>{{ scope.$index+1 }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="abstractContent"
          label="摘要">
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.abstractContent"
              placeholder="请选择"
              clearable filterable
              @blur="selectMethod"

            >
              <el-option
                v-for="(item,index) in AbstractData"
                :key="index"
                :label="item.abstractContent"
                :value="item.abstractContent"
                @click="AddAbstractContent(scope.row.abstractContent)"/>
            </el-select>
          </template>



        </el-table-column>
        <el-table-column
          prop="name"
          label="会计科目">
<!--          <el-select-->
<!--            v-model="form.accountingAccount"-->
<!--            placeholder="请选择"-->
<!--            clearable filterable-->
<!--            @blur="selectMethod">-->

<!--            <el-option-->
<!--              v-for="(item,index) in options"-->
<!--              :key="index"-->
<!--              :label="item.label"-->
<!--              :value="item.value" />-->
<!--          </el-select>-->
          <template slot-scope="scope">
          <el-select v-model="scope.row.accountingAccount" placeholder="请选择科目" >
            <el-option
              v-for="item in ArrayData"
              :key="item.subjectId"
              :label="item.subjectName"
              :value="item.subjectId">
            </el-option>
          </el-select>
          </template>

        </el-table-column>
        <el-table-column
          prop="amount1"
          label="借方金额">
          <el-input v-model="form.debitAmount" placeholder="请输入借方金额" />
        </el-table-column>
        <el-table-column
          prop="amount2"
          label="贷方金额">
          <el-input v-model="form.creditAmount" placeholder="请输入贷方金额" />
        </el-table-column>
      </el-table>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProof, getProof, delProof, addProof, updateProof , FindWordProof , FindAbstractProof } from "@/api/proof/proof";
import {subjectListCash} from "@/api/cash/cash";

export default {
  name: "Proof",
  data() {
    return {
      tableData: [{},{},{},{}],
      activeName: 'first',
      wordData:{},
      AbstractData:{},
      ArrayData:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 凭证设置表格数据
      proofList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 审核时间时间范围
      daterangeProofTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        proofTime: null,
        abstractContent: null,
        proofWords: null,
        accountingAccount: null,
        documenter: null,
        auditor: null,
        proofStatus: null,
      },
      // 表单参数
      form: {
        abstractContent:[]
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.showWord();
    this.showAbstract();
    this.showAccpt(0);
  },
  methods: {

    AddData(row){
      this.form.accountingAccount = row.subjectName
    },

    handleCurrentChange(val) {
      this.currentRow = val;
      console.log("currentRow"+val);
    },

    showAccpt(id){
      subjectListCash(id).then(response => {
        this.ArrayData = response.rows;
        console.log("ArrayData"+this.ArrayData)
      });
    },


    AddAbstractContent(data){
      this.form.abstractContent = data;
      console.log("form:"+this.form)
    },
    qxchange(index){
      // console.log(index)
      // console.log(this.form.abstractContent)
    },
    showWord(){
      FindWordProof().then(response => {
        this.wordData = response.rows;
        console.log("wordData:"+this.wordData)
      });
    },

    showAbstract(){
      FindAbstractProof().then(response => {
        this.AbstractData = response.rows;
        console.log("AbstractData:"+this.AbstractData)
      })
    },


    AddTable(){
      //列表添加一行
      console.log("eiji")
      this.tableData.push({})
    },


    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总价';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] += ' 元';
        } else {
          sums[index] = 'N/A';
        }
      });

      return sums;

    },

    selectMethod(e) {

      // 意见类型
      console.log(e)
      if (e.target.value !== '') {
        this.data = e.target.value; //将this.data换成自己的就可以使用
        this.$forceUpdate(); // 强制更新
      }
    },

    /** 查询凭证设置列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeProofTime && '' != this.daterangeProofTime) {
        this.queryParams.params["beginProofTime"] = this.daterangeProofTime[0];
        this.queryParams.params["endProofTime"] = this.daterangeProofTime[1];
      }
      listProof(this.queryParams).then(response => {
        this.proofList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        proofId: null,
        proofTime: null,
        abstractContent: null,
        proofWords: null,
        accountingAccount: null,
        debitAmount: null,
        creditAmount: null,
        attachments: null,
        documenter: null,
        auditor: null,
        proofStatus: null,
        entryTime: null,
        auditTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeProofTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加凭证设置";
      this.form.entryTime = new Date();
      this.form.documenter = this.$store.state.user.name;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProof(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改凭证设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form)
      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if (this.form.id != null) {
      //       updateProof(this.form).then(response => {
      //         this.$modal.msgSuccess("修改成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     } else {
      //       addProof(this.form).then(response => {
      //         this.$modal.msgSuccess("新增成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     }
      //   }
      // });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除凭证设置编号为"' + ids + '"的数据项？').then(function() {
        return delProof(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('proof/proof/export', {
        ...this.queryParams
      }, `proof_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
